package com.zqf.shiro.pojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;

/**
 *
 * @author Hero
 * @date 7/12/2017
 */
@Api("返回结果集")
public class Result<T> {

    @ApiModelProperty("返回码")
    private int code;
    @ApiModelProperty("返回消息")
    private String message;
    @ApiModelProperty("数据")
    private T data;

    public static <T> Result<T> build() {
        return new Result<T>();
    }

    private Result() {
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Result<T> withCode(int code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Result<T> withMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Result<T> withData(T data) {
        this.code =  200;
        this.message = "SUCCESS";
        this.data = data;
        return this;
    }

    public static <T> Result<T> buildSuccess(T data){
        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(200);
        result.setMessage("SUCCESS");
        return result;
    }

    public static <T> Result<T> success(){
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage("SUCCESS");
        return result;
    }
    /**
     * 操作成功
     *
     * @return this
     */
    public Result<T> ok() {
        this.code = 200;
        this.message = "SUCCESS";
        return this;
    }

    /**
     * 系统内部错误
     *
     * @return this
     */
    public static<T> Result<T> error(int code,String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * 操作错误
     *
     * @return this
     */
    public Result<T> error(Integer code) {
        this.code = code;
        this.message = "FAIL";
        return this;
    }


    private <T extends Throwable> Result<T> resultFormat(Integer code, T ex) {
        ex.printStackTrace();
        return Result.error(code, ex.getMessage());
    }



}
